import pytest
import allure

from api_test.testcases.calendar.BaseCalendarTestCase import BaseCalendarTestCase
from api_test.utils.data_loader import DataLoader

data = DataLoader.load_case_data('calendar', 'calendar.yml')['create_calendar']
ids = [d.pop(0) for d in data]

@allure.feature("日历模块")
@pytest.mark.parametrize("summary, description, permissions, color, summary_alias, http_code, api_code", data, ids=ids)
class TestCalendar(BaseCalendarTestCase):

    # def setup(self):
        # if self.calendarapihttp.calendar_ids:
        #     for calendar_id in self.calendarapihttp.calendar_ids:
        #         self.calendarapihttp.delete(calendar_id)
        # self.calendarapihttp.calendar_ids = []
        # print("\n数据前置OK! ")

    def teardown_class(self):
        # print(f"\nteardown {self.calendarapihttp.calendar_ids}")
        for calendar_id in self.calendarapihttp.calendar_ids:
            res = self.calendarapihttp.delete(calendar_id)
            # print(res)
            if res.get('code') != 0:
                if res.get('code') != 191003:
                    print(f"\n{calendar_id} 删除失败 {res}")
                    return f"\n{calendar_id} 删除失败 {res}"
        print("\n数据后置OK! ")

    # 1.参数化 + 数据驱动 2.纯数据驱动， 把用例步骤也数据化（如httprunner)
    @allure.story("创建日历")
    @pytest.mark.smoke
    def test_create(self, summary, description, permissions, color, summary_alias, http_code, api_code):
        res = self.calendarapihttp.create(summary, description, permissions, color, summary_alias)
        # print(res)
        assert res[0] == http_code and res[1].get('code') == api_code and \
               len(res[1]['data']["calendar"]["calendar_id"]) == 57

    @allure.story("根据id查询日历")
    def test_get(self, summary, description, permissions, color, summary_alias, http_code, api_code):
        #1 创建数据
        res = self.calendarapihttp.create(summary, description, permissions, color, summary_alias, http_code, api_code)
        #2 根据calendar_id查询日历
        res = self.calendarapihttp.get(self.calendarapihttp.calendar_ids[-1])
        assert res.get('code') == 0 and res['data']["summary"] == str(summary)

    @allure.story("查询所有日历")
    def test_list(self, summary, description, permissions, color, summary_alias, http_code, api_code):
        # 创建数据
        res = self.calendarapihttp.create(summary, description, permissions, color, summary_alias, http_code, api_code)
        # 查询日历列表
        res = self.calendarapihttp.list()
        # print(res)
        # 获取日历ID列表复制到delete.yaml可用于手动删除
        items = res['data']["calendar_list"]
        # print(len(items))
        assert res.get('code') == 0 and len(res['data']["calendar_list"]) >= 1

    @allure.story("根据id删除日历")
    def test_delete(self, summary, description, permissions, color, summary_alias, http_code, api_code):
        """删除日历"""
        # 创建数据后
        res = self.calendarapihttp.create(summary, description, permissions, color, summary_alias, http_code, api_code)
        # 删除日历
        res = self.calendarapihttp.delete(self.calendarapihttp.calendar_ids[-1])
        # print(res)
        assert res.get('code') == 0

    # @allure.story("根据id更新日历")
    # @pytest.mark.parametrize("summary, description, permissions, color, summary_alias, http_code, api_code", updatedata)
    # def test_update(self, summary, description, permissions, color, summary_alias, http_code, api_code):
    #     # 创建数据
    #     res = self.calendarapihttp.create(summary, description, permissions, color, summary_alias, http_code, api_code)
    #     # 更新日历
    #     calendar_id = self.calendarapihttp.calendar_ids[-1]
    #     res = self.calendarapihttp.update(calendar_id, summary, description, permissions, color, summary_alias, http_code, api_code)
    #     # print(res)
    #     assert res.get('code') == 0

    # if '__name__' == '__main__':
    #     pytest.main(["calendar_test.py"])